package com.ky.manage.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * TimeUtil
 * Created by panghuan on 2018/8/9
 * 时间工具类
 */

public class TimeUtil {
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sdftime = new SimpleDateFormat("HH:mm:ss");
    private static final SimpleDateFormat hmtime = new SimpleDateFormat("HH:mm");

    /**
     * 获取前一天的日期
     *
     * @return
     */
    public static synchronized String getLastDate(String currentDate) {
        String dateString = "";
        try {
            Date date = simpleDateFormat.parse(currentDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1);
            date = calendar.getTime();
            dateString = format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateString;
    }

    /**
     * 获取时间
     *
     * @return
     */
    public static String getHmsTime(String currentDate) {
        String dateString = "";
        try {
            if (!TextUtils.isEmpty(currentDate)) {
                Date date = simpleDateFormat.parse(currentDate);
                dateString = sdftime.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateString;
    }

    /**
     * 获取时间 hm
     *
     * @return
     */
    public static String getHmTime(String currentDate) {
        String dateString = "";
        try {
            if (!TextUtils.isEmpty(currentDate)) {
                Date date = simpleDateFormat.parse(currentDate);
                dateString = hmtime.format(date);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return StringUtil.getNoYmdTime(System.currentTimeMillis());
        }
        return dateString;
    }

    /**
     * 比较两个String类型的日期
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean compareDate(String date1, String date2) {
        boolean result = false;
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        try {
            Date todayDate = df.parse(date1);
            Date lastDate = df.parse(date2);
            if (todayDate.getTime() > lastDate.getTime()) {
                result = true;
            } else if (todayDate.getTime() <= lastDate.getTime()) {
                result = false;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

    /**
     * 比较两个String类型的日期差值
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long compareDateValue(String date1, String date2) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            if (!TextUtils.isEmpty(date1) && !TextUtils.isEmpty(date2)) {
                Date todayDate = df.parse(date1);
                Date lastDate = df.parse(date2);
                return Math.abs(todayDate.getTime() - lastDate.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getDate(int time) {
        return new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date(((long) (time)) * 1000L));
    }

    public static String getDate(long time) {
        return new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(new Date(time));
    }

    /**
     * 埋点日期
     *
     * @param time
     * @return
     */
    public static String getStatisticDate(long time) {
        return new SimpleDateFormat("yyyyMMddHH", Locale.getDefault()).format(new Date(time));
    }

    public static String getAllDate(long time) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(time));
    }

    public static String getYMDDate(int time) {
        return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date(((long) (time)) * 1000L));
    }

    public static String getYMDDate(long time) {
        return new SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(new Date(time));
    }

    public static int getDateSecond(String dateStr) {
        int seconds = 0;
        if (TextUtils.isEmpty(dateStr) || "FFFFFFFFFFFFFF".equals(dateStr)) {
            return seconds;
        }
        try {
            Date date = simpleDateFormat.parse(dateStr);
            seconds = (int) (date.getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return seconds;
    }

    public static String getHMSTime(int time) {
        if (time == 0) {
            return "----";
        } else {
            return new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date(((long) (time)) * 1000L));
        }

    }

    public static int getDateSecondYMD(String dateStr) {
        int seconds = 0;
        try {
            if (!TextUtils.isEmpty(dateStr)) {
                Date date = format.parse(dateStr);
                seconds = (int) (date.getTime() / 1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return seconds;
    }

    /**
     * 将现在的正常字符串格式时间转换成距离1970的数字时间
     * 比如字符串格式时间："2017-12-15 21:49:03"
     * 转换后的数字时间："1513345743"
     *
     * @param time
     * @return
     */
    public static long StringTimeToSecond(String time) {
        long d1time = 0;
        try {
            d1time = sdf.parse(time).getTime() / 1000;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d1time;
    }

    public static String getYmdHmsTimeFromDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }
}
